package estore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class ConsoleShop3 {

    private final ProductService3 productService3 = new ProductService3();
    private final Map<String, Integer> shoppingCart = new HashMap<>();
    private final Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消费掉换行符

            switch (choice) {
                case 1:
                    browseProducts();
                    break;
                case 2:
                    addToCart();
                    break;
                case 3:
                    viewCart();
                    break;
                case 4:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n===== 欢迎来到商城 =====");
        System.out.println("1. 浏览所有商品");
        System.out.println("2. 添加商品到购物车");
        System.out.println("3. 查看购物车");
        System.out.println("4. 退出");
        System.out.print("请输入您的选择: ");
    }

    private void browseProducts() {
        System.out.println("\n--- 商品列表 ---");
        List<Product3> product3s = productService3.getAllProducts();
        if (product3s.isEmpty()) {
            System.out.println("商店里还没有商品。");
        } else {
            // 显示商品数量信息
            System.out.println("共找到 " + product3s.size() + " 个商品：");
            product3s.forEach(System.out::println);

            // 验证标签唯一性（测试用）
            validateTagUniqueness();
        }
    }

    // 验证标签唯一性的方法
    private void validateTagUniqueness() {
        System.out.println("\n--- 标签验证 ---");
        List<Product3> product3s = productService3.getAllProducts();
        for (Product3 product3 : product3s) {
            if (!product3.getTags().isEmpty()) {
                System.out.println("商品 '" + product3.getName() + "' 的标签数量: " +
                        product3.getTags().size() + ", 标签: " + product3.getTags());
            }
        }
    }

    private void addToCart() {
        System.out.print("请输入要添加的商品ID: ");
        String productId = scanner.nextLine();
        Product3 product3 = productService3.findProductById(productId);

        if (product3 == null) {
            System.out.println("错误：找不到该商品！");
            return;
        }
        if (product3.getStock() <= 0) {
            System.out.println("抱歉，该商品已售罄。");
            return;
        }

        System.out.print("请输入购买数量: ");
        int quantity = scanner.nextInt();
        scanner.nextLine(); // 消费换行符

        if (quantity <= 0) {
            System.out.println("错误：购买数量必须为正数！");
            return;
        }


        if (quantity > product3.getStock()) {
            System.out.println("错误：库存不足！当前库存为 " + product3.getStock());
            return;
        }

        shoppingCart.put(productId, shoppingCart.getOrDefault(productId, 0) + quantity);
        System.out.println("成功将 '" + product3.getName() + "' 添加到购物车！");
    }

    private void viewCart() {
        System.out.println("\n--- 我的购物车 ---");
        if (shoppingCart.isEmpty()) {
            System.out.println("购物车是空的，快去逛逛吧！");
            return;
        }

        double total = 0.0;
        for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {
            String productId = entry.getKey();
            Integer quantity = entry.getValue();
            Product3 product3 = productService3.findProductById(productId);
            if (product3 != null) {
                double subtotal = product3.getPrice() * quantity;
                System.out.printf("商品: %-20s | 单价: ¥%-8.2f | 数量: %-3d | 小计: ¥%.2f\n",
                        product3.getName(), product3.getPrice(), quantity, subtotal);
                total += subtotal;
            }
        }
        System.out.println("------------------------------------");
        System.out.printf("总计: ¥%.2f\n", total);
    }

    public static void main(String[] args) {
        new ConsoleShop3().start();
    }
}